package com.example.administrator.exkotlin.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_test__2.*

class Test_Activity_2 : BaseActivity() {
    private val TAG = "Test_Activity_2"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test__2)
        initData()
        recevieData()
    }

    private fun recevieData() {
        val stringExtra: String? = intent.getStringExtra("test_string__send_extra")
        Log.i("Test_Activity_2", "收到数据$stringExtra")
        tv_title.text = stringExtra?.toString() ?: "没有传值"
    }

    private fun initData() {
        btn_1.setOnClickListener {
            startActivity(Intent("com.test.activitytest.ACTION_START"))
        }
        btn_2.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)

        }
        btn_3.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)

        }

        /**
         * 返回数据给之前的页面
         * */
        btn_4.setOnClickListener {
            retrunExtra()

        }

    }

    private fun retrunExtra() {
        val intent = Intent()
        intent.putExtra("test_string__return_extra", "2返回数据")
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun onBackPressed() {
        Log.i("Test_Activity_2", "onBackPressed")
        retrunExtra()
    }

    /**
     *  静态块，类似于java中的静态方法
     *  采用这种方式启动Activity，利于管理，清晰知道参数
     * */
    companion object {
        fun startActivity2(context: Context?, data1: String, data2: String) {
            val intent = Intent(context, Test_Activity_2::class.java).apply {
                putExtra("test_string__send_extra", data1)
                putExtra("test_string__send_extra2", data1)
            }
            context?.startActivity(intent)
        }
    }
}
